package com.qingcheng.service.impl;

import com.qingcheng.dao.SensitiveWordsMapper;
import com.qingcheng.pojo.comment.SensitiveWords;
import com.qingcheng.util.AllComment;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class CommmentServiceInit implements InitializingBean {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SensitiveWordsMapper sensitiveWordsMapper;

    @Override
    public void afterPropertiesSet() throws Exception {
        List<SensitiveWords> sensitiveWords = sensitiveWordsMapper.selectAll();
        try {
            if (sensitiveWords.size() > 0 && sensitiveWords != null){
                //说明数据库不为空
                redisTemplate.boundValueOps(AllComment.ShowSitiveWordsIsNull).set("1");
                //循环遍历,以id为key,sensitiveWords为值存入redis的AllSitiveWords为名的hashredis
                for (SensitiveWords sensitiveWord : sensitiveWords) {
                    redisTemplate.boundSetOps(AllComment.AllSitiveWords).add(sensitiveWord);
                }
                redisTemplate.boundValueOps(AllComment.addSitiveWords).set("0");
                redisTemplate.boundValueOps(AllComment.deleteSitiveWords).set("0");
                redisTemplate.boundValueOps(AllComment.returnSitiveWords).set("0");
                redisTemplate.boundValueOps(AllComment.selectAllSitiveWords).set("0");
                redisTemplate.boundValueOps(AllComment.selectSomeSitiveWords).set("0");
            }else{
                redisTemplate.boundValueOps(AllComment.ShowSitiveWordsIsNull).set("0");
                throw new RuntimeException("数据库敏感词汇为空");
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
    }
}
